(function(){
    require.config({
        paths: {
            Framework7:'framework7/framework7',
            Jquery:'jquery/jquery-1.11.2.min'

        },
        shim: {
            'Framework7':{exports: 'Framework7'}
        }
    });

    require(['Framework7','router','globalService','pageCtrl'], function (Framework7,router,GS,PC) {
        window.templates = {
            showOrderTemplate : Template7.compile($('#show-order-template').html()),
            showOrderInfoTemplate : Template7.compile($('#show-order-info-template').html()),
            showPersonInfoTemplate : Template7.compile($('#show-person-info-template').html()),
            showServiceTemplate : Template7.compile($('#show-service-template').html()),
            showServiceInfoTemplate : Template7.compile($('#show-service-info-template').html()),
            showMessageTemplate : Template7.compile($('#show-message-template').html()),
            showMessageContentTemplate : Template7.compile($('#show-message-content-template').html())
        };
        window.appMain ={
           init:function(){
               window.yunSpaceApp = new Framework7({
                   //cache:false,
                   pushState:true,
                   swipePanel:'left',
                   pushStateSeparator:'#page/',
                   swipePanelThreshold:20,
                   swipeBackPageAnimateShadow:false,//页面返回特效
                   swipeBackPageAnimateOpacity:false,//页面返回特效
                   swipeBackPageActiveArea:500,
                   template7Pages: true,
                   template7Data: {
                       'page:order':{
                           //'name':'<a style="display: none">lihanbn</a>'
                       }
                   }
               })
               window.$$ = Dom7;
               window.mainView = yunSpaceApp.addView('#mainView', {
                   dynamicNavbar: true,
                   //cache:false,
                   pushState:true
               });
               window.messageView = yunSpaceApp.addView('#messageView', {
                   dynamicNavbar: true,
                   //cache:false,
                   pushState:true
               });
               window.serviceView = yunSpaceApp.addView('#serviceView', {
                   dynamicNavbar: true,
                   //cache:false,
                   pushState:true
               });


               router.init();
               $('#logOut').on('click', PC.logOut);

               $('#serviceView').on('show',function(){
                   if($('#serviceList li').size()<=0){
                       PC.serviceList();
                   }
               });
               
               $('#messageView').on('show', function () {
                   if($('#messageList li').size()<=0){
                       PC.getMessages();
                   }
               });
               document.addEventListener('deviceready', this.onDeviceReady, false);
               document.addEventListener("backbutton", this.onBackKeyDown, false);
           },
            onDeviceReady: function() {
                navigator.splashscreen.hide(); //隐藏介绍屏
            },
            onBackKeyDown:function(){
                if(!window.location.hash||window.location.hash=='#page/order.html'){
                    navigator.notification.confirm(
                        '确定要退出吗?',  // message
                        appMain.onConfirm,              // callback to invoke with index of button pressed
                        '提示',            // title
                        '退出,取消'          // buttonLabels
                    );

                }else{
                    //navigator.app.backHistory();
                    window.history.go(-1);
                    //mainView.router.loadPage('order.html');
                    //yunSpaceApp.showTab('#mainView');
                    //yunSpaceApp.showToolbar();
                }
            },
            //exitApp:function(){
            //    navigator.app.exitApp();
            //},
            onConfirm:function(button){
                if(button==1){
                    navigator.app.exitApp();
                }
            },
            login:function(){
                var username = $.trim($('#username').val().trim());
                var password = $.trim($('#password').val().trim());
                if(!username||!password) {
                    yunSpaceApp.alert('用户名和密码不能为空！！','提示');
                    return false;
                }
                yunSpaceApp.showIndicator();


                var url =appMain.apiRoot+'vendor/login';
                var data = {
                    'username':username,
                    'password':password
                };
                $$.ajax({
                    url:url,
                    type:'post',
                    dataType:'json',
                    data:data,
                    success: function (data) {
                        if(data.status=='success'){
                            if(data.login_time) localStorage.setItem("login_time",data.login_time);
                            if(data.user_id) localStorage.setItem("user_id",data.user_id);
                            if(data.token) localStorage.setItem("user_token",data.token);
                            $('.myName').text(data.username);
                            $('.myAccount span').text(data.fullname);
                            PC.homePageData(data.user_id);

                        }else{
                            yunSpaceApp.alert('登录失败！！','提示')
                            yunSpaceApp.hideIndicator();
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest.status);
                        console.log(textStatus);
                        console.log(errorThrown);
                        yunSpaceApp.hideIndicator();
                    }
                })
                return false
            },
            //apiRoot : 'http://test.iyunspace.com.cn/app_api/'
            apiRoot: 'http://localhost/yun/Yun-Space/app_api/'
        }
        appMain.init();

    })

})()
